package com.多用户通讯编程.QQ.Server.QQservice;

import com.IO流.处理流.PrintWriter__打印流.PrintWriter__;
import com.多用户通讯编程.QQ.Common.Message;

import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

public class ManageOfflineMessages {
    private static ConcurrentHashMap<String, Vector<Message>> concurrentHashMap = new ConcurrentHashMap<>();



    public static ConcurrentHashMap<String, Vector<Message>> getConcurrentHashMap() {
        return concurrentHashMap;
    }

    public static void setConcurrentHashMap(ConcurrentHashMap<String, Vector<Message>> concurrentHashMap) {
        ManageOfflineMessages.concurrentHashMap = concurrentHashMap;
    }

    /**
     * 将消息对象放入HashMap中对应的键下
     * 如果该键尚无消息队列，则创建一个新的Vector存储该消息
     * 如果该键已有消息队列，则将消息添加到现有的Vector中
     *
     * @param key 消息的键，用于标识消息属于哪一类或哪个接收者
     * @param message 要存储的消息对象
     */
    public static void putMessage(String key , Message message) {
        /// 从HashMap中获取与键关联的消息队列
        Vector<Message> messageVector = concurrentHashMap.get(key);
        if (messageVector == null){
            /// 如果当前键没有对应的消息队列，则创建一个新的Vector对象并添加消息
            Vector<Message> messages = new Vector<>();
            messages.add(message);
            /// 将新的消息队列放入HashMap中
            concurrentHashMap.put(key,messages);
        }else {
            /// 如果当前键已有消息队列，则直接添加消息到队列中
            messageVector.add(message);
        }
    }


    public static Vector<Message> getMessages(String userId){
        return concurrentHashMap.get(userId);
    }

    public static void remove(String userId){
        concurrentHashMap.remove(userId);
    }


}
